home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_urllib2.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  28KB  |  902 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import unittest
  5. from test import test_support
  6. import os
  7. import socket
  8. import StringIO
  9. import urllib2
  10. from urllib2 import Request, OpenerDirector
  11.  
  12. class TrivialTests(unittest.TestCase):
  13.     
  14.     def test_trivial(self):
  15.         self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')
  16.         fname = os.path.abspath(urllib2.__file__).replace('\\', '/')
  17.         if fname[1:2] == ':':
  18.             fname = fname[2:]
  19.         
  20.         if os.name == 'mac':
  21.             fname = '/' + fname.replace(':', '/')
  22.         elif os.name == 'riscos':
  23.             import string as string
  24.             fname = os.expand(fname)
  25.             fname = fname.translate(string.maketrans('/.', './'))
  26.         
  27.         file_url = 'file://%s' % fname
  28.         f = urllib2.urlopen(file_url)
  29.         buf = f.read()
  30.         f.close()
  31.  
  32.     
  33.     def test_parse_http_list(self):
  34.         tests = [
  35.             ('a,b,c', [
  36.                 'a',
  37.                 'b',
  38.                 'c']),
  39.             ('path"o,l"og"i"cal, example', [
  40.                 'path"o,l"og"i"cal',
  41.                 'example']),
  42.             ('a, b, "c", "d", "e,f", g, h', [
  43.                 'a',
  44.                 'b',
  45.                 '"c"',
  46.                 '"d"',
  47.                 '"e,f"',
  48.                 'g',
  49.                 'h']),
  50.             ('a="b\\"c", d="e\\,f", g="h\\\\i"', [
  51.                 'a="b"c"',
  52.                 'd="e,f"',
  53.                 'g="h\\i"'])]
  54.         for string, list in tests:
  55.             self.assertEquals(urllib2.parse_http_list(string), list)
  56.         
  57.  
  58.  
  59.  
  60. class MockOpener:
  61.     addheaders = []
  62.     
  63.     def open(self, req, data = None):
  64.         self.req = req
  65.         self.data = data
  66.  
  67.     
  68.     def error(self, proto, *args):
  69.         self.proto = proto
  70.         self.args = args
  71.  
  72.  
  73.  
  74. class MockFile:
  75.     
  76.     def read(self, count = None):
  77.         pass
  78.  
  79.     
  80.     def readline(self, count = None):
  81.         pass
  82.  
  83.     
  84.     def close(self):
  85.         pass
  86.  
  87.  
  88.  
  89. class MockHeaders(dict):
  90.     
  91.     def getheaders(self, name):
  92.         return self.values()
  93.  
  94.  
  95.  
  96. class MockResponse(StringIO.StringIO):
  97.     
  98.     def __init__(self, code, msg, headers, data, url = None):
  99.         StringIO.StringIO.__init__(self, data)
  100.         (self.code, self.msg, self.headers, self.url) = (code, msg, headers, url)
  101.  
  102.     
  103.     def info(self):
  104.         return self.headers
  105.  
  106.     
  107.     def geturl(self):
  108.         return self.url
  109.  
  110.  
  111.  
  112. class MockCookieJar:
  113.     
  114.     def add_cookie_header(self, request):
  115.         self.ach_req = request
  116.  
  117.     
  118.     def extract_cookies(self, response, request):
  119.         self.ec_req = request
  120.         self.ec_r = response
  121.  
  122.  
  123.  
  124. class FakeMethod:
  125.     
  126.     def __init__(self, meth_name, action, handle):
  127.         self.meth_name = meth_name
  128.         self.handle = handle
  129.         self.action = action
  130.  
  131.     
  132.     def __call__(self, *args):
  133.         return self.handle(self.meth_name, self.action, *args)
  134.  
  135.  
  136.  
  137. class MockHandler:
  138.     
  139.     def __init__(self, methods):
  140.         self._define_methods(methods)
  141.  
  142.     
  143.     def _define_methods(self, methods):
  144.         for spec in methods:
  145.             if len(spec) == 2:
  146.                 (name, action) = spec
  147.             else:
  148.                 name = spec
  149.                 action = None
  150.             meth = FakeMethod(name, action, self.handle)
  151.             setattr(self.__class__, name, meth)
  152.         
  153.  
  154.     
  155.     def handle(self, fn_name, action, *args, **kwds):
  156.         self.parent.calls.append((self, fn_name, args, kwds))
  157.         if action is None:
  158.             return None
  159.         elif action == 'return self':
  160.             return self
  161.         elif action == 'return response':
  162.             res = MockResponse(200, 'OK', { }, '')
  163.             return res
  164.         elif action == 'return request':
  165.             return Request('http://blah/')
  166.         elif action.startswith('error'):
  167.             code = action[action.rfind(' ') + 1:]
  168.             
  169.             try:
  170.                 code = int(code)
  171.             except ValueError:
  172.                 pass
  173.  
  174.             res = MockResponse(200, 'OK', { }, '')
  175.             return self.parent.error('http', args[0], res, code, '', { })
  176.         elif action == 'raise':
  177.             raise urllib2.URLError('blah')
  178.         
  179.  
  180.     
  181.     def close(self):
  182.         pass
  183.  
  184.     
  185.     def add_parent(self, parent):
  186.         self.parent = parent
  187.         self.parent.calls = []
  188.  
  189.     
  190.     def __lt__(self, other):
  191.         if not hasattr(other, 'handler_order'):
  192.             return True
  193.         
  194.         return self.handler_order < other.handler_order
  195.  
  196.  
  197.  
  198. def add_ordered_mock_handlers(opener, meth_spec):
  199.     '''Create MockHandlers and add them to an OpenerDirector.
  200.  
  201.     meth_spec: list of lists of tuples and strings defining methods to define
  202.     on handlers.  eg:
  203.  
  204.     [["http_error", "ftp_open"], ["http_open"]]
  205.  
  206.     defines methods .http_error() and .ftp_open() on one handler, and
  207.     .http_open() on another.  These methods just record their arguments and
  208.     return None.  Using a tuple instead of a string causes the method to
  209.     perform some action (see MockHandler.handle()), eg:
  210.  
  211.     [["http_error"], [("http_open", "return request")]]
  212.  
  213.     defines .http_error() on one handler (which simply returns None), and
  214.     .http_open() on another handler, which returns a Request object.
  215.  
  216.     '''
  217.     handlers = []
  218.     count = 0
  219.     for meths in meth_spec:
  220.         
  221.         class MockHandlerSubclass(MockHandler):
  222.             pass
  223.  
  224.         h = MockHandlerSubclass(meths)
  225.         h.handler_order = count
  226.         h.add_parent(opener)
  227.         count = count + 1
  228.         handlers.append(h)
  229.         opener.add_handler(h)
  230.     
  231.     return handlers
  232.  
  233.  
  234. class OpenerDirectorTests(unittest.TestCase):
  235.     
  236.     def test_handled(self):
  237.         o = OpenerDirector()
  238.         meth_spec = [
  239.             [
  240.                 'http_open',
  241.                 'ftp_open',
  242.                 'http_error_302'],
  243.             [
  244.                 'ftp_open'],
  245.             [
  246.                 ('http_open', 'return self')],
  247.             [
  248.                 ('http_open', 'return self')]]
  249.         handlers = add_ordered_mock_handlers(o, meth_spec)
  250.         req = Request('http://example.com/')
  251.         r = o.open(req)
  252.         self.assertEqual(r, handlers[2])
  253.         calls = [
  254.             (handlers[0], 'http_open'),
  255.             (handlers[2], 'http_open')]
  256.         for expected, got in zip(calls, o.calls):
  257.             (handler, name, args, kwds) = got
  258.             self.assertEqual((handler, name), expected)
  259.             self.assertEqual(args, (req,))
  260.         
  261.  
  262.     
  263.     def test_handler_order(self):
  264.         o = OpenerDirector()
  265.         handlers = []
  266.         for meths, handler_order in [
  267.             ([
  268.                 ('http_open', 'return self')], 500),
  269.             ([
  270.                 'http_open'], 0)]:
  271.             
  272.             class MockHandlerSubclass(MockHandler):
  273.                 pass
  274.  
  275.             h = MockHandlerSubclass(meths)
  276.             h.handler_order = handler_order
  277.             handlers.append(h)
  278.             o.add_handler(h)
  279.         
  280.         r = o.open('http://example.com/')
  281.         self.assertEqual(o.calls[0][0], handlers[1])
  282.         self.assertEqual(o.calls[1][0], handlers[0])
  283.  
  284.     
  285.     def test_raise(self):
  286.         o = OpenerDirector()
  287.         meth_spec = [
  288.             [
  289.                 ('http_open', 'raise')],
  290.             [
  291.                 ('http_open', 'return self')]]
  292.         handlers = add_ordered_mock_handlers(o, meth_spec)
  293.         req = Request('http://example.com/')
  294.         self.assertRaises(urllib2.URLError, o.open, req)
  295.         self.assertEqual(o.calls, [
  296.             (handlers[0], 'http_open', (req,), { })])
  297.  
  298.     
  299.     def test_http_error(self):
  300.         o = OpenerDirector()
  301.         meth_spec = [
  302.             [
  303.                 ('http_open', 'error 302')],
  304.             [
  305.                 ('http_error_400', 'raise'),
  306.                 'http_open'],
  307.             [
  308.                 ('http_error_302', 'return response'),
  309.                 'http_error_303',
  310.                 'http_error'],
  311.             [
  312.                 'http_error_302']]
  313.         handlers = add_ordered_mock_handlers(o, meth_spec)
  314.         
  315.         class Unknown:
  316.             
  317.             def __eq__(self, other):
  318.                 return True
  319.  
  320.  
  321.         req = Request('http://example.com/')
  322.         r = o.open(req)
  323.         calls = [
  324.             (handlers[0], 'http_open', (req,)),
  325.             (handlers[2], 'http_error_302', (req, Unknown(), 302, '', { }))]
  326.         for expected, got in zip(calls, o.calls):
  327.             (handler, method_name, args) = expected
  328.             self.assertEqual((handler, method_name), got[:2])
  329.             self.assertEqual(args, got[2])
  330.         
  331.  
  332.     
  333.     def test_processors(self):
  334.         o = OpenerDirector()
  335.         meth_spec = [
  336.             [
  337.                 ('http_request', 'return request'),
  338.                 ('http_response', 'return response')],
  339.             [
  340.                 ('http_request', 'return request'),
  341.                 ('http_response', 'return response')]]
  342.         handlers = add_ordered_mock_handlers(o, meth_spec)
  343.         req = Request('http://example.com/')
  344.         r = o.open(req)
  345.         calls = [
  346.             (handlers[0], 'http_request'),
  347.             (handlers[1], 'http_request'),
  348.             (handlers[0], 'http_response'),
  349.             (handlers[1], 'http_response')]
  350.         for handler, name, args, kwds in enumerate(o.calls):
  351.             if i < 2:
  352.                 self.assertEqual((handler, name), calls[i])
  353.                 self.assertEqual(len(args), 1)
  354.                 self.assert_(isinstance(args[0], Request))
  355.                 continue
  356.             self.assertEqual((handler, name), calls[i])
  357.             self.assertEqual(len(args), 2)
  358.             self.assert_(isinstance(args[0], Request))
  359.             if not args[1] is None:
  360.                 pass
  361.             self.assert_(isinstance(args[1], MockResponse))
  362.         
  363.  
  364.  
  365.  
  366. def sanepathname2url(path):
  367.     import urllib as urllib
  368.     urlpath = urllib.pathname2url(path)
  369.     if os.name == 'nt' and urlpath.startswith('///'):
  370.         urlpath = urlpath[2:]
  371.     
  372.     return urlpath
  373.  
  374.  
  375. class HandlerTests(unittest.TestCase):
  376.     
  377.     def test_ftp(self):
  378.         
  379.         class MockFTPWrapper:
  380.             
  381.             def __init__(self, data):
  382.                 self.data = data
  383.  
  384.             
  385.             def retrfile(self, filename, filetype):
  386.                 self.filename = filename
  387.                 self.filetype = filetype
  388.                 return (StringIO.StringIO(self.data), len(self.data))
  389.  
  390.  
  391.         
  392.         class NullFTPHandler(urllib2.FTPHandler):
  393.             
  394.             def __init__(self, data):
  395.                 self.data = data
  396.  
  397.             
  398.             def connect_ftp(self, user, passwd, host, port, dirs):
  399.                 self.user = user
  400.                 self.passwd = passwd
  401.                 self.host = host
  402.                 self.port = port
  403.                 self.dirs = dirs
  404.                 self.ftpwrapper = MockFTPWrapper(self.data)
  405.                 return self.ftpwrapper
  406.  
  407.  
  408.         import ftplib as ftplib
  409.         import socket as socket
  410.         data = 'rheum rhaponicum'
  411.         h = NullFTPHandler(data)
  412.         o = h.parent = MockOpener()
  413.         for url, host, port, type_, dirs, filename, mimetype in [
  414.             ('ftp://localhost/foo/bar/baz.html', 'localhost', ftplib.FTP_PORT, 'I', [
  415.                 'foo',
  416.                 'bar'], 'baz.html', 'text/html'),
  417.             ('ftp://localhost:80/foo/bar/', 'localhost', 80, 'D', [
  418.                 'foo',
  419.                 'bar'], '', None),
  420.             ('ftp://localhost/baz.gif;type=a', 'localhost', ftplib.FTP_PORT, 'A', [], 'baz.gif', None)]:
  421.             r = h.ftp_open(Request(url))
  422.             None(self.assert_ if h.passwd == h.passwd else h.passwd == '')
  423.             self.assertEqual(h.host, socket.gethostbyname(host))
  424.             self.assertEqual(h.port, port)
  425.             self.assertEqual(h.dirs, dirs)
  426.             self.assertEqual(h.ftpwrapper.filename, filename)
  427.             self.assertEqual(h.ftpwrapper.filetype, type_)
  428.             headers = r.info()
  429.             self.assertEqual(headers.get('Content-type'), mimetype)
  430.             self.assertEqual(int(headers['Content-length']), len(data))
  431.         
  432.  
  433.     
  434.     def test_file(self):
  435.         import time as time
  436.         import rfc822 as rfc822
  437.         import socket
  438.         h = urllib2.FileHandler()
  439.         o = h.parent = MockOpener()
  440.         TESTFN = test_support.TESTFN
  441.         urlpath = sanepathname2url(os.path.abspath(TESTFN))
  442.         towrite = 'hello, world\n'
  443.         for url in [
  444.             'file://localhost%s' % urlpath,
  445.             'file://%s' % urlpath,
  446.             'file://%s%s' % (socket.gethostbyname('localhost'), urlpath),
  447.             'file://%s%s' % (socket.gethostbyname(socket.gethostname()), urlpath)]:
  448.             f = open(TESTFN, 'wb')
  449.             
  450.             try:
  451.                 
  452.                 try:
  453.                     f.write(towrite)
  454.                 finally:
  455.                     f.close()
  456.  
  457.                 r = h.file_open(Request(url))
  458.                 
  459.                 try:
  460.                     data = r.read()
  461.                     headers = r.info()
  462.                     newurl = r.geturl()
  463.                 finally:
  464.                     r.close()
  465.  
  466.                 stats = os.stat(TESTFN)
  467.                 modified = rfc822.formatdate(stats.st_mtime)
  468.             finally:
  469.                 os.remove(TESTFN)
  470.  
  471.             self.assertEqual(data, towrite)
  472.             self.assertEqual(headers['Content-type'], 'text/plain')
  473.             self.assertEqual(headers['Content-length'], '13')
  474.             self.assertEqual(headers['Last-modified'], modified)
  475.         
  476.         for url in [
  477.             'file://localhost:80%s' % urlpath]:
  478.             
  479.             try:
  480.                 f = open(TESTFN, 'wb')
  481.                 
  482.                 try:
  483.                     f.write(towrite)
  484.                 finally:
  485.                     f.close()
  486.  
  487.                 self.assertRaises(urllib2.URLError, h.file_open, Request(url))
  488.             finally:
  489.                 os.remove(TESTFN)
  490.  
  491.         
  492.         h = urllib2.FileHandler()
  493.         o = h.parent = MockOpener()
  494.         for url, ftp in [
  495.             ('file://ftp.example.com//foo.txt', True),
  496.             ('file://ftp.example.com///foo.txt', False),
  497.             ('file://ftp.example.com/foo.txt', False)]:
  498.             req = Request(url)
  499.             
  500.             try:
  501.                 h.file_open(req)
  502.             except (urllib2.URLError, OSError):
  503.                 self.assert_(not ftp)
  504.                 continue
  505.  
  506.             self.assert_(o.req is req)
  507.             self.assertEqual(req.type, 'ftp')
  508.         
  509.  
  510.     
  511.     def test_http(self):
  512.         
  513.         class MockHTTPResponse:
  514.             
  515.             def __init__(self, fp, msg, status, reason):
  516.                 self.fp = fp
  517.                 self.msg = msg
  518.                 self.status = status
  519.                 self.reason = reason
  520.  
  521.             
  522.             def read(self):
  523.                 return ''
  524.  
  525.  
  526.         
  527.         class MockHTTPClass:
  528.             
  529.             def __init__(self):
  530.                 self.req_headers = []
  531.                 self.data = None
  532.                 self.raise_on_endheaders = False
  533.  
  534.             
  535.             def __call__(self, host):
  536.                 self.host = host
  537.                 return self
  538.  
  539.             
  540.             def set_debuglevel(self, level):
  541.                 self.level = level
  542.  
  543.             
  544.             def request(self, method, url, body = None, headers = { }):
  545.                 self.method = method
  546.                 self.selector = url
  547.                 self.req_headers += headers.items()
  548.                 if body:
  549.                     self.data = body
  550.                 
  551.                 if self.raise_on_endheaders:
  552.                     import socket
  553.                     raise socket.error()
  554.                 
  555.  
  556.             
  557.             def getresponse(self):
  558.                 return MockHTTPResponse(MockFile(), { }, 200, 'OK')
  559.  
  560.  
  561.         h = urllib2.AbstractHTTPHandler()
  562.         o = h.parent = MockOpener()
  563.         url = 'http://example.com/'
  564.         for method, data in [
  565.             ('GET', None),
  566.             ('POST', 'blah')]:
  567.             req = Request(url, data, {
  568.                 'Foo': 'bar' })
  569.             req.add_unredirected_header('Spam', 'eggs')
  570.             http = MockHTTPClass()
  571.             r = h.do_open(http, req)
  572.             r.read
  573.             r.readline
  574.             r.info
  575.             r.geturl
  576.             (r.code, r.msg == 200, 'OK')
  577.             hdrs = r.info()
  578.             hdrs.get
  579.             hdrs.has_key
  580.             self.assertEqual(r.geturl(), url)
  581.             self.assertEqual(http.host, 'example.com')
  582.             self.assertEqual(http.level, 0)
  583.             self.assertEqual(http.method, method)
  584.             self.assertEqual(http.selector, '/')
  585.             self.assertEqual(http.req_headers, [
  586.                 ('Connection', 'close'),
  587.                 ('Foo', 'bar'),
  588.                 ('Spam', 'eggs')])
  589.             self.assertEqual(http.data, data)
  590.         
  591.         http.raise_on_endheaders = True
  592.         self.assertRaises(urllib2.URLError, h.do_open, http, req)
  593.         o.addheaders = [
  594.             ('Spam', 'eggs')]
  595.         for data in ('', None):
  596.             req = Request('http://example.com/', data)
  597.             r = MockResponse(200, 'OK', { }, '')
  598.             newreq = h.do_request_(req)
  599.             if data is None:
  600.                 self.assert_('Content-length' not in req.unredirected_hdrs)
  601.                 self.assert_('Content-type' not in req.unredirected_hdrs)
  602.             else:
  603.                 self.assertEqual(req.unredirected_hdrs['Content-length'], '0')
  604.                 self.assertEqual(req.unredirected_hdrs['Content-type'], 'application/x-www-form-urlencoded')
  605.             self.assertEqual(req.unredirected_hdrs['Host'], 'example.com')
  606.             self.assertEqual(req.unredirected_hdrs['Spam'], 'eggs')
  607.             req.add_unredirected_header('Content-length', 'foo')
  608.             req.add_unredirected_header('Content-type', 'bar')
  609.             req.add_unredirected_header('Host', 'baz')
  610.             req.add_unredirected_header('Spam', 'foo')
  611.             newreq = h.do_request_(req)
  612.             self.assertEqual(req.unredirected_hdrs['Content-length'], 'foo')
  613.             self.assertEqual(req.unredirected_hdrs['Content-type'], 'bar')
  614.             self.assertEqual(req.unredirected_hdrs['Host'], 'baz')
  615.             self.assertEqual(req.unredirected_hdrs['Spam'], 'foo')
  616.         
  617.  
  618.     
  619.     def test_errors(self):
  620.         h = urllib2.HTTPErrorProcessor()
  621.         o = h.parent = MockOpener()
  622.         url = 'http://example.com/'
  623.         req = Request(url)
  624.         r = MockResponse(200, 'OK', { }, '', url)
  625.         newr = h.http_response(req, r)
  626.         self.assert_(r is newr)
  627.         self.assert_(not hasattr(o, 'proto'))
  628.         r = MockResponse(201, 'Created', { }, '', url)
  629.         self.assert_(h.http_response(req, r) is None)
  630.         self.assertEqual(o.proto, 'http')
  631.         self.assertEqual(o.args, (req, r, 201, 'Created', { }))
  632.  
  633.     
  634.     def test_cookies(self):
  635.         cj = MockCookieJar()
  636.         h = urllib2.HTTPCookieProcessor(cj)
  637.         o = h.parent = MockOpener()
  638.         req = Request('http://example.com/')
  639.         r = MockResponse(200, 'OK', { }, '')
  640.         newreq = h.http_request(req)
  641.         None(self.assert_ if req is req else req is newreq)
  642.         self.assertEquals(req.get_origin_req_host(), 'example.com')
  643.         self.assert_(not req.is_unverifiable())
  644.         newr = h.http_response(req, r)
  645.         self.assert_(cj.ec_req is req)
  646.         None(self.assert_ if r is r else r is newr)
  647.  
  648.     
  649.     def test_redirect(self):
  650.         from_url = 'http://example.com/a.html'
  651.         to_url = 'http://example.com/b.html'
  652.         h = urllib2.HTTPRedirectHandler()
  653.         o = h.parent = MockOpener()
  654.         for code in (301, 302, 303, 307):
  655.             for data in (None, 'blah\nblah\n'):
  656.                 method = getattr(h, 'http_error_%s' % code)
  657.                 req = Request(from_url, data)
  658.                 req.add_header('Nonsense', 'viking=withhold')
  659.                 req.add_unredirected_header('Spam', 'spam')
  660.                 
  661.                 try:
  662.                     method(req, MockFile(), code, 'Blah', MockHeaders({
  663.                         'location': to_url }))
  664.                 except urllib2.HTTPError:
  665.                     if code == 307:
  666.                         pass
  667.                     self.assert_(data is not None)
  668.  
  669.                 self.assertEqual(o.req.get_full_url(), to_url)
  670.                 
  671.                 try:
  672.                     self.assertEqual(o.req.get_method(), 'GET')
  673.                 except AttributeError:
  674.                     self.assert_(not o.req.has_data())
  675.  
  676.                 self.assertEqual(o.req.headers['Nonsense'], 'viking=withhold')
  677.                 self.assert_('Spam' not in o.req.headers)
  678.                 self.assert_('Spam' not in o.req.unredirected_hdrs)
  679.             
  680.         
  681.         req = Request(from_url)
  682.         
  683.         def redirect(h, req, url = to_url):
  684.             h.http_error_302(req, MockFile(), 302, 'Blah', MockHeaders({
  685.                 'location': url }))
  686.  
  687.         req = Request(from_url, origin_req_host = 'example.com')
  688.         count = 0
  689.         
  690.         try:
  691.             while None:
  692.                 count = count + 1
  693.         except urllib2.HTTPError:
  694.             self.assertEqual(count, urllib2.HTTPRedirectHandler.max_repeats)
  695.  
  696.         req = Request(from_url, origin_req_host = 'example.com')
  697.         count = 0
  698.         
  699.         try:
  700.             while None:
  701.                 count = count + 1
  702.         except urllib2.HTTPError:
  703.             self.assertEqual(count, urllib2.HTTPRedirectHandler.max_redirections)
  704.  
  705.  
  706.     
  707.     def test_cookie_redirect(self):
  708.         
  709.         class MockHTTPHandler(urllib2.HTTPHandler):
  710.             
  711.             def __init__(self):
  712.                 self._count = 0
  713.  
  714.             
  715.             def http_open(self, req):
  716.                 import mimetools as mimetools
  717.                 StringIO = StringIO
  718.                 import StringIO
  719.                 if self._count == 0:
  720.                     self._count = self._count + 1
  721.                     msg = mimetools.Message(StringIO('Location: http://www.cracker.com/\r\n\r\n'))
  722.                     return self.parent.error('http', req, MockFile(), 302, 'Found', msg)
  723.                 else:
  724.                     self.req = req
  725.                     msg = mimetools.Message(StringIO('\r\n\r\n'))
  726.                     return MockResponse(200, 'OK', msg, '', req.get_full_url())
  727.  
  728.  
  729.         CookieJar = CookieJar
  730.         import cookielib
  731.         build_opener = build_opener
  732.         HTTPHandler = HTTPHandler
  733.         HTTPError = HTTPError
  734.         HTTPCookieProcessor = HTTPCookieProcessor
  735.         import urllib2
  736.         interact_netscape = interact_netscape
  737.         import test_cookielib
  738.         cj = CookieJar()
  739.         interact_netscape(cj, 'http://www.example.com/', 'spam=eggs')
  740.         hh = MockHTTPHandler()
  741.         cp = HTTPCookieProcessor(cj)
  742.         o = build_opener(hh, cp)
  743.         o.open('http://www.example.com/')
  744.         self.assert_(not hh.req.has_header('Cookie'))
  745.  
  746.  
  747.  
  748. class MiscTests(unittest.TestCase):
  749.     
  750.     def test_build_opener(self):
  751.         
  752.         class MyHTTPHandler(urllib2.HTTPHandler):
  753.             pass
  754.  
  755.         
  756.         class FooHandler(urllib2.BaseHandler):
  757.             
  758.             def foo_open(self):
  759.                 pass
  760.  
  761.  
  762.         
  763.         class BarHandler(urllib2.BaseHandler):
  764.             
  765.             def bar_open(self):
  766.                 pass
  767.  
  768.  
  769.         build_opener = urllib2.build_opener
  770.         o = build_opener(FooHandler, BarHandler)
  771.         self.opener_has_handler(o, FooHandler)
  772.         self.opener_has_handler(o, BarHandler)
  773.         o = build_opener(FooHandler, BarHandler())
  774.         self.opener_has_handler(o, FooHandler)
  775.         self.opener_has_handler(o, BarHandler)
  776.         o = build_opener(MyHTTPHandler)
  777.         self.opener_has_handler(o, MyHTTPHandler)
  778.         o = build_opener()
  779.         self.opener_has_handler(o, urllib2.HTTPHandler)
  780.         o = build_opener(urllib2.HTTPHandler)
  781.         self.opener_has_handler(o, urllib2.HTTPHandler)
  782.         o = build_opener(urllib2.HTTPHandler())
  783.         self.opener_has_handler(o, urllib2.HTTPHandler)
  784.  
  785.     
  786.     def opener_has_handler(self, opener, handler_class):
  787.         for h in opener.handlers:
  788.             if h.__class__ == handler_class:
  789.                 break
  790.                 continue
  791.         
  792.  
  793.  
  794.  
  795. class NetworkTests(unittest.TestCase):
  796.     
  797.     def setUp(self):
  798.         pass
  799.  
  800.     
  801.     def test_range(self):
  802.         req = urllib2.Request('http://www.python.org', headers = {
  803.             'Range': 'bytes=20-39' })
  804.         result = urllib2.urlopen(req)
  805.         data = result.read()
  806.         self.assertEqual(len(data), 20)
  807.  
  808.     
  809.     def test_ftp(self):
  810.         urls = [
  811.             'ftp://www.python.org/pub/python/misc/sousa.au',
  812.             'ftp://www.python.org/pub/tmp/blat',
  813.             'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/00README-Legal-Rules-Regs']
  814.         self._test_urls(urls, self._extra_handlers())
  815.  
  816.     
  817.     def test_gopher(self):
  818.         urls = [
  819.             'gopher://gopher.lib.ncsu.edu/11/library/stacks/Alex',
  820.             'gopher://gopher.vt.edu:10010/10/33']
  821.         self._test_urls(urls, self._extra_handlers())
  822.  
  823.     
  824.     def test_file(self):
  825.         TESTFN = test_support.TESTFN
  826.         f = open(TESTFN, 'w')
  827.         
  828.         try:
  829.             f.write('hi there\n')
  830.             f.close()
  831.             urls = [
  832.                 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
  833.                 ('file://nonsensename/etc/passwd', None, (OSError, socket.error))]
  834.             self._test_urls(urls, self._extra_handlers())
  835.         finally:
  836.             os.remove(TESTFN)
  837.  
  838.  
  839.     
  840.     def test_http(self):
  841.         urls = [
  842.             'http://www.espn.com/',
  843.             'http://www.python.org/Spanish/Inquistion/',
  844.             ('http://www.python.org/cgi-bin/faqw.py', 'query=pythonistas&querytype=simple&casefold=yes&req=search', None),
  845.             'http://www.python.org/']
  846.         self._test_urls(urls, self._extra_handlers())
  847.  
  848.     
  849.     def _test_urls(self, urls, handlers):
  850.         import socket
  851.         import time
  852.         import logging as logging
  853.         debug = logging.getLogger('test_urllib2').debug
  854.         urllib2.install_opener(urllib2.build_opener(*handlers))
  855.         for url in urls:
  856.             if isinstance(url, tuple):
  857.                 (url, req, expected_err) = url
  858.             else:
  859.                 req = None
  860.                 expected_err = None
  861.             debug(url)
  862.             
  863.             try:
  864.                 f = urllib2.urlopen(url, req)
  865.             except (IOError, socket.error, OSError):
  866.                 err = None
  867.                 debug(err)
  868.                 if expected_err:
  869.                     self.assert_(isinstance(err, expected_err))
  870.                 
  871.             except:
  872.                 expected_err
  873.  
  874.             buf = f.read()
  875.             f.close()
  876.             debug('read %d bytes' % len(buf))
  877.             debug('******** next url coming up...')
  878.             time.sleep(0.10000000000000001)
  879.         
  880.  
  881.     
  882.     def _extra_handlers(self):
  883.         handlers = []
  884.         handlers.append(urllib2.GopherHandler)
  885.         cfh = urllib2.CacheFTPHandler()
  886.         cfh.setTimeout(1)
  887.         handlers.append(cfh)
  888.         return handlers
  889.  
  890.  
  891.  
  892. def test_main(verbose = None):
  893.     tests = (TrivialTests, OpenerDirectorTests, HandlerTests, MiscTests)
  894.     if test_support.is_resource_enabled('network'):
  895.         tests += (NetworkTests,)
  896.     
  897.     test_support.run_unittest(*tests)
  898.  
  899. if __name__ == '__main__':
  900.     test_main(verbose = True)
  901.  
  902.